package demo;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

// 时间线动画(由多个关键帧组成,关键帧由关键值或事件组成,可以实现其余动画)
public class TimelineDemo extends Application {
	private Pane root = new Pane();
	private Rectangle rectangle;
	private Timeline timeline;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		// 初始旋转角度和旋转点的坐标(图形的左上角为(0,0);3D图形也可以设置,图形的中心为原点;缩放点也可以设置)
		Rotate rotate = new Rotate(0, 0, 50);	
		
	    rectangle = new Rectangle(50, 50);
	    rectangle.getTransforms().add(rotate);
	    rectangle.setLayoutX(150);
	    rectangle.setLayoutY(100);
		root.getChildren().add(rectangle);
	    
		// 动画
		// 实现绕任意点旋转(要用到Timeline,还可实现任意点缩放)
		timeline = new Timeline(
			new KeyFrame(Duration.ZERO, new KeyValue(rotate.angleProperty(), 0)),		// 关键帧周期的起点都是0
			new KeyFrame(Duration.ZERO, e -> rectangle.setFill(Color.color(Math.random(), Math.random(), Math.random()))),
			new KeyFrame(Duration.seconds(4), new KeyValue(rotate.angleProperty(), 360))		
		);
		timeline.setCycleCount(Timeline.INDEFINITE);
		timeline.play();
		
	    Scene scene = new Scene(root, 500, 400);    
	    stage.setScene(scene);
		stage.show();
	}
}
